﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Bullet : MonoBehaviour {
	
	public List<GameObject> targetLine;
	public int color;
	
	public GameCore gameCore;
	
	// Use this for initialization
	void Start () {
		GameObject gameCoreObject = GameObject.Find("GameCore");
		gameCore = gameCoreObject.GetComponent<GameCore>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	IEnumerator OnTriggerEnter (Collider col) {
		
		EKTween.Kill(gameObject); //Stop move
		
		GameObject hitObject = col.gameObject;
		
		//Bullet's explosion effect
		GameObject explosion = Instantiate(GameCore.gameManager.explosionNormal) as GameObject;
		explosion.transform.position = gameObject.transform.position;
		renderer.enabled = false;
		
		GameObject targetEnemy = targetLine[targetLine.Count-1];
		if(hitObject.tag == "EnemyShip" && hitObject == targetEnemy) { //Hit target enemy's ship?
			
			Enemy enemy = hitObject.GetComponent<Enemy>();
			if(enemy.color == color) { //Same color?
				yield return StartCoroutine(gameCore.DestroyLine(targetLine, UI.currentSelectionLine));
			}
		}
		
		//Clear destroyed line
		UI.ClearCurrentLine();
		//UI.currentLine.Clear();
		
		Destroy(gameObject);
		
		GameCore.CheckRemainRow();
		
		//Generate new bullet color
		GameCore.gameManager.GenerateRandomColor();
	}
	
}
